﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Solidi
{
	public static class DependencyRegistrarExtensions
	{
		public static IDependencyDecorator RegisterType<TType>(this IDependencyRegistrar source)
		{
			return RegisterType(source, typeof(TType));
		}

		public static IDependencyDecorator RegisterFactory<TType>(this IDependencyRegistrar source, Func<TType> factory)
		{
			return RegisterComposer(source, typeof(TType), new DependencyFactoryComposer(container => factory()));
		}

		public static IDependencyDecorator RegisterFactory<TType>(this IDependencyRegistrar source, Func<IDependencyContainer, TType> factory)
		{
			return RegisterComposer(source, typeof(TType), new DependencyFactoryComposer(container => factory(container)));
		}

		public static IDependencyDecorator RegisterType(this IDependencyRegistrar source, Type type)
		{
			return RegisterComposer(source, type, new DependencyTypeComposer(type));
		}

		public static IDependencyDecorator RegisterFactory(this IDependencyRegistrar source, Type type, Func<IDependencyContainer, object> factory)
		{
			return RegisterComposer(source, type, new DependencyFactoryComposer(factory));
		}

		public static IDependencyDecorator RegisterComposer(this IDependencyRegistrar source, Type type, IDependencyComposer composer)
		{
			IDependencyKey key = new DependencyKey(type);

			IDependencyRegistration dependency = new DependencyRegistration(key, composer);

			return source.Register(dependency);
		}
	}
}
